<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <meta name="description" content="TingGe Blog Agile agile JavaScript NodeJS">
  <meta name="author" content="TingGe<505253293@163.com>">
  <title>听歌实验室</title>
  <link type="image/x-icon" rel="shortcut icon" href="https://avatars3.githubusercontent.com/u/4074517?v=3&s=40" />
  <link rel="stylesheet" type="text/css" href="/lib/bootstrap/dist/css/bootstrap.min.css" media="screen">
  <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css">
  <link href="/css/landing-page.css" rel="stylesheet">
  <link href="/css/screenshots.css" rel="stylesheet">
  <style media="screen">
    #main {
      padding-top: 55px;
    }
  </style>
  <script type="text/javascript" src="/lib/jquery/dist/jquery.min.js"></script>
  <script type="text/javascript" src="/lib/html2canvas/dist/html2canvas.min.js"></script>
  <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
  <!-- Navigation -->
  <nav class="navbar navbar-default navbar-fixed-top topnav" role="navigation">
    <div class="container topnav">
      <!-- Brand and toggle get grouped for better mobile display -->
      <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
          <span class="sr-only">Toggle navigation</span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
          <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand topnav" href="/">
          <i class="fa fa-flask icon-3x" aria-hidden="true"></i> 听歌实验室
        </a>
      </div>
      <!-- Collect the nav links, forms, and other content for toggling -->
      <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
        <ul class="nav navbar-nav navbar-right">
          <li>
            <a href="/html/js.html">JavaScript/NodeJS</a>
          </li>
          <li>
            <a href="/index.html#agile">敏捷/项目管理(Agile/PMP)</a>
          </li>
          <li>
            <a href="/html/cloud-bi.html">云商业智能(Cloud BI)</a>
          </li>
          <li>
            <a href="/html/iot.html">物联网(WoT/IoT)</a>
          </li>
          <li>
            <a href="/html/me.html">关于我</a>
          </li>
        </ul>
      </div>
      <!-- /.navbar-collapse -->
    </div>
    <!-- /.container -->
  </nav>
  <div id="main" class="container">
    <div class="row">
      <div id="article" class="col-md-9 content-container">
        <h1 id="jetson-tk1-tegra-k1-mipi-csi-2-ov5640">Jetson TK1 Tegra K1 摄像头 MIPI CSI-2 模块 OV5640</h1>
<p>[TOC]</p>
<p>TK1 Camera MIPI CSI-2 模块实物图</p>
<p><img src="../img/scam-tk1.png" alt="TK1 Camera MIPI CSI-2 模块实物图"></p>
<h2 id="-">安装过程</h2>
<h3 id="-0-kernel-">准备 0 下载 Kernel 和 驱动资源</h3>
<pre><code class="lang-shell">cd ~ &amp;&amp; mkdir tegra &amp;&amp; cd ~/tegra
wget http://developer.download.nvidia.com/embedded/L4T/r21_Release_v4.0/source/kernel_src.tbz2
wget http://developer.download.nvidia.com/embedded/L4T/r21_Release_v4.0/Tegra124_Linux_R21.4.0_armhf.tbz2
wget http://developer.download.nvidia.com/embedded/L4T/r21_Release_v4.0/Tegra_Linux_Sample-Root-Filesystem_R21.4.0_armhf.tbz2
</code></pre>
<h3 id="-1-release-linux_for_tegra">步骤 1 解压 release 包，组合 Linux_for_Tegra</h3>
<pre><code class="lang-shell">sudo tar -xjf Tegra124_Linux_R21.4.0_armhf.tbz2
cd Linux_for_Tegra/rootfs
sudo tar -xjf /home/ubuntu/tegra/Tegra_Linux_Sample-Root-Filesystem_R21.4.0_armhf.tbz2
</code></pre>
<h3 id="-2-">步骤 2 执行安装脚本</h3>
<pre><code class="lang-shell">cd ..
export LDK_ROOTFS_DIR=/home/ubuntu/tegra/Linux_for_Tegra/rootfs
sudo ./apply_binaries.sh
</code></pre>
<h3 id="-3-arm-">步骤 3 获取 ARM 交叉编译工具和依赖包</h3>
<pre><code class="lang-shell">sudo apt-get install gcc-arm-linux-gnueabihf build-essential
</code></pre>
<h3 id="-4-kernel">步骤 4 解压 kernel</h3>
<pre><code class="lang-shell">cd ~/tegra
tar -xjf kernel_src.tbz2
cd kernel/
</code></pre>
<h3 id="-5-config">步骤 5 配置 .config</h3>
<pre><code class="lang-shell">ARCH=arm make tegra12_defconfig
ARCH=arm make menuconfig
</code></pre>
<blockquote>
<p>如 <code>ARCH=arm make menuconfig</code> 执行失败，需 <code>apt-get install libncurses5-dev</code></p>
</blockquote>
<p>然后进行配置：</p>
<ol>
<li><p><strong>开启</strong> 下面 1 项（这里选 M）</p>
<pre><code> Device Drivers &gt; Multimedia support &gt; Sensors used on soc_camera driver &gt; ov5640 camera support
</code></pre></li>
<li><p><strong>关闭</strong>下面 2 项</p>
<pre><code> Device Drivers &gt; Multimedia support &gt; V4L platform devices &gt; OV5640 camera sensor support
 Device Drivers &gt; Graphics support &gt; Tegra video input host1x client driver
</code></pre></li>
<li><p>然后保存退出</p>
</li>
</ol>
<h3 id="-6-">步骤 6 设置环境变量</h3>
<pre><code class="lang-shell">export LDK_ROOTFS_DIR=/home/ubuntu/tegra/Linux_for_Tegra/rootfs/
export LDK_DIR=/home/ubuntu/tegra/Linux_for_Tegra/
</code></pre>
<h3 id="-7-">步骤 7 交叉编译</h3>
<p><code>ln -s ~/tegra/kernel/include/dt-bindings ~/tegra/kernel/arch/arm/boot/dts/include</code></p>
<p>执行下列命令</p>
<pre><code class="lang-shell">ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make zImage -j4
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make modules -j4
ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make dtbs -j4
sudo ARCH=arm CROSS_COMPILE=arm-linux-gnueabihf- make modules_install INSTALL_MOD_PATH=$LDK_ROOTFS_DIR
sudo cp arch/arm/boot/zImage $LDK_ROOTFS_DIR/boot
sudo cp arch/arm/boot/zImage $LDK_DIR/kernel/zImage
sudo cp arch/arm/boot/dts/tegra124-jetson_tk1-pm375-000-c00-00.dtb $LDK_DIR/kernel/dtb/
sudo cp arch/arm/boot/dts/tegra124-jetson_tk1-pm375-000-c00-00.dtb $LDK_ROOTFS_DIR/boot/
</code></pre>
<h3 id="-8-l4t-">步骤 8 烧写L4T系统</h3>
<ol>
<li>装上 SCam-TK1 模块，usb 连接 开发机和 TK1。</li>
<li>按住开发板上头的 FROC ERECOVERY 同时按下 Reset 键 进入恢复模式...</li>
<li>开发机上勾选 Nvidia Corp 的 usb 连接。如不确定，可执行 <code>lsusb</code> 查看是否有  Nvidia Corp 设备。</li>
<li>确定以上通过后执行下面语句，开始烧录。烧录一般需要一个小时左右。</li>
</ol>
<pre><code class="lang-shell">cd $LDK_DIR/
sudo ./flash.sh jetson-tk1 mmcblk0p1
</code></pre>
<p><strong>注意事项：</strong>注意开发机磁盘空间是否够用。预防因磁盘空间不足中断。</p>
<h3 id="-9-tegra_camera-">步骤 9 验证 tegra_camera 驱动</h3>
<ol>
<li>刷完以后，登录 TK1 机器，进入开发板桌面系统</li>
<li>执行 <code>sudo modprobe tegra_camera</code></li>
<li>至此 摄像头驱动加载完成，可以用 V4L2 访问摄像头</li>
<li>可以用</li>
</ol>
<pre><code class="lang-shell">gst-launch-0.10 -v v4l2src queue-size=1 ! &#39;video/x-raw-yuv,format=(fourcc)UYVY,width=1920,height=1080&#39; ! xvimagesink
</code></pre>
<p>命令进行测试。</p>
<h2 id="-">参考</h2>
<ul>
<li><a href="https://developer.nvidia.com/linux-tegra-r214">Linux For Tegra R21.4</a></li>
<li><a href="https://developer.nvidia.com/embedded/downloads">Nvidia Embedded Download Center</a></li>
<li><a href="http://wiki.oz9aec.net/index.php/Gstreamer_cheat_sheet#Webcam_Capture">Gstreamer cheat sheet</a></li>
<li><a href="https://linux.cn/article-3587-1.html">Linux系统中 ‘dmesg’ 命令处理故障和收集系统信息的7种用法</a></li>
<li><a href="http://www.mr-wu.cn/ubuntu-video4linux2-v4l2-development-library/">Ubuntu Video4Linux2 (v4l2) 开发库安装</a></li>
<li><a href="https://www.howtoinstall.co/en/ubuntu/trusty/i2c-tools">How to install i2c-tools on Ubuntu 14.04 (Trusty Tahr)</a></li>
</ul>
<h2 id="-">常用调试</h2>
<ol>
<li><p>确认是否内核编译过且刷成功</p>
</li>
<li><p>查看 dev 下面有几个 video 开头的设备 </p>
<pre><code class="lang-shell">ls /dev/video*
</code></pre>
</li>
<li><p>测测 i2c 电压是否正常，正常值是 1.8</p>
</li>
<li><p>拿 i2c tools 看看能扫到 ID 不 </p>
<pre><code class="lang-shell">sudo apt-get update
sudo apt-get install i2c-tools
i2cdetect -y 0
</code></pre>
</li>
<li><p><code>lsmod</code> 查看都加载了哪些模块</p>
</li>
<li><p>重启 TK1 然后以 root 身份执行</p>
<pre><code class="lang-shell">su
echo 20 &gt; /proc/sys/kernel/printk 
modprobe tegra_camera
gst-launch-0.10 -v v4l2src queue-size=1 ! &#39;video/x-raw-yuv,format=(fourcc)UYVY,width=1920,height=1080&#39; ! xvimagesink
</code></pre>
</li>
</ol>
<p>注意事项：Ubuntu 下设定 root 密码 <code>sudo passwd root</code></p>

      </div>
      <div class="col-md-3" style="padding-top:20px;">
        <iframe src="http://githubbadge.appspot.com/tingge?a=0" style="border: 0;height: 142px;width: 200px;overflow: hidden;" frameBorder="0"></iframe>
      </div>
    </div>
  </div>
  <a id="html2canvas" title="截图下载-快照">&nbsp;</a>
  <script src="/lib/bootstrap/dist/js/bootstrap.min.js"></script>
  <script src="/js/article.js" charset="utf-8"></script>
  <!-- justice.js https://github.com/okor/justice  -->
  <script src="/lib/justice.min.js" charset="utf-8"></script>
  <!-- instantclick -->
  <script src="/lib/instantclick.js" charset="utf-8"></script>
  <script type="text/javascript" data-no-instant>
    Justice.init();
  </script>
</body>

</html>
